package org.xtense.model.actions;

import org.dom4j.Attribute;
import org.dom4j.CDATA;
import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.Entity;
import org.dom4j.Namespace;
import org.dom4j.ProcessingInstruction;
import org.dom4j.Text;
import org.dom4j.Visitor;
import org.xtense.engine.actions.UpdateAction;

public class RemoveVisitor implements Visitor {
	private int documentID;

	public RemoveVisitor(int documentID) {
		this.documentID = documentID;
	}

	@Override
	public void visit(Document document) {

	}

	@Override
	public void visit(DocumentType documentType) {

	}

	@Override
	public void visit(Element node) {
		int elementPre = ResultCache.getElementPreFromNode(node);
		if (elementPre < 0) {
			elementPre = UpdateAction.getInstance(documentID).getCache()
					.getPre(node)[0];
		}
		UpdateAction.getInstance(documentID).getCache().cacheSql(
				UpdateHelper.getInstance()
						.removeElement(documentID, elementPre));
		
		if (node.getTextTrim().length() > 0) {
			UpdateAction.getInstance(documentID).getCache().cacheSql(
					UpdateHelper.getInstance().removeText(documentID,
							elementPre));
		}

	}

	@Override
	public void visit(Attribute node) {
		int elementPre = ResultCache.getElementPreFromNode(node.getParent());
		if (elementPre < 0) {
			elementPre = UpdateAction.getInstance(documentID).getCache()
					.getPre(node.getParent())[0];
		}
		UpdateAction.getInstance(documentID).getCache().cacheSql(
				UpdateHelper.getInstance()
						.removeAttribute(documentID,
						elementPre, node.getName()));

	}

	@Override
	public void visit(CDATA node) {

	}

	@Override
	public void visit(Comment node) {

	}

	@Override
	public void visit(Entity node) {

	}

	@Override
	public void visit(Namespace namespace) {

	}

	@Override
	public void visit(ProcessingInstruction node) {

	}

	@Override
	public void visit(Text node) {

	}

}
